package company

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"jwgin/model/mysql"
	"jwgin/service"
)

type exchange struct {
}

func (l *exchange) Routers(Router *gin.RouterGroup) (R gin.IRoutes) {
	Router.GET("exchange", l.get) //兑换记录
	return Router
}

func (exchange) get(c *gin.Context) {
	count, size, offset := pages(c)
	comid := c.GetUint("RoleID")
	if comid == 0 {
		RespErr(c, fmt.Errorf("系统用户认证失败"))
		return
	}

	db := service.G.Model(&mysql.Exchange{}).
		Joins("JOIN `user` ON user.id = exchange.uid").
		Joins("JOIN `gift` ON gift.id = exchange.gid").
		Select("gift.name,gift.quota,user.account,exchange.*").
		Where("comid = ?", comid)
	//订单id
	if id := c.Query("eid"); id != "" {
		db = db.Where("eid = ?", id)
	}

	//订单状态
	if ty := c.Query("type"); ty != "" {
		db = db.Where("type = ?", ty)
	}

	//订单手机号
	if mob := c.Query("mobile"); mob != "" {
		db = db.Where("mobile = ?", mob)
	}

	//时间
	startTime := c.Query("startTime")
	endTime := c.Query("endTime")
	if startTime != "" && endTime != "" {
		db = db.Where(" `addtime` > ? AND `addtime` < ? ", startTime, endTime)
	}
	ret := []map[string]interface{}{}
	err := db.Count(&count).Offset(offset).Limit(size).Order("exchange.addtime DESC").Scan(&ret).Error
	if err != nil {
		RespErr(c, err)
		return
	}
	RespList(c, count, ret)
}
